class_name Goal
extends Node2D

## 球门的位置
enum GoalLocation {
	## 主场位置
	HOME,
	## 客场位置
	AWAY
}


@onready var back_net_area: Area2D = $BackNetArea
@onready var shooting_position_node: Node2D = $ShootingPositionNode

## 球门的在球场上的位置
@export var goal_location: GoalLocation
## 顶部射门位置的偏移量，用于计算顶部射门的坐标
@export var top_shooting_offset: float = -30.0
## 底部射门位置的偏移量，用于计算底部射门的坐标
@export var bottom_shooting_offset: float = 30.0

# 射门时的顶部位置坐标
var _top_shooting_position_y: float
# 射门时的底部位置坐标
var _bottom_shooting_position_y: float




func _ready():
	back_net_area.body_entered.connect(on_ball_enter_back_net)
	_shooting_global_positions()



func _shooting_global_positions():
	_top_shooting_position_y = shooting_position_node.global_position.y + top_shooting_offset
	_bottom_shooting_position_y = shooting_position_node.global_position.y + bottom_shooting_offset


## 从球门的射门点随机获取一个射门方向
func get_random_target_direction(player: Node2D) -> Vector2:
	var y := randf_range(_top_shooting_position_y, _bottom_shooting_position_y)
	var point := Vector2(shooting_position_node.global_position.x, y)
	return player.global_position.direction_to(point)




func on_ball_enter_back_net(body: Node2D):
	if body is Ball:
		body.stop()
